/**
*   FuzzyEngine - an Simple flex framework 
*   Copyright (C) 2010 Constantin Aldea
*
*   This program is free software: you can redistribute it and/or modify
*   it under the terms of the GNU General Public License as published by
*   the Free Software Foundation, either version 3 of the License, or
*   (at your option) any later version.
*   This program is distributed in the hope that it will be useful,
*   but WITHOUT ANY WARRANTY; without even the implied warranty of
*   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*   GNU General Public License for more details.
*
*   You should have received a copy of the GNU General Public License
*   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package fuzzy.penelope.core.dataservices.delegates {
	import fuzzy.penelope.model.ContextManager;
	import fuzzy.penelope.model.vo.framework.access.Persona;
	import mx.controls.Alert;
	import mx.events.CloseEvent;
	import mx.rpc.CallResponder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;

	public class DeleteDelegate extends EventDispatcher
	{
		
		private static var instance:DeleteDelegate;
		
		public function DeleteDelegate(target:IEventDispatcher=null)
		{
			super(target);
			if(instance != null){
				throw new Error("e un singleton");
			}
			
		}
		
		public static function getInstance():DeleteDelegate{
			if(DeleteDelegate.instance == null){
				instance = new DeleteDelegate();
			}
			return instance;
		}
				private var _responder:CallResponder;
				private var _module:Class;
		
		public static function confirmService(id:String, module:Class, responder:CallResponder=null, cancelFunctionOverride:Function=null) : void
		{   
   			 // the show method follows the method signature from the Flex Actionscript API
   			DeleteDelegate.getInstance().cancelFunction = cancelFunctionOverride;
   			DeleteDelegate.getInstance().module = module;
   			DeleteDelegate.getInstance().id =  id;
   			DeleteDelegate.getInstance().responder = responder;
			DeleteDelegate.getInstance().createConfirmationAlert();
		}
		
		private function createConfirmationAlert():void{
				Alert.yesLabel = "Si";
    			Alert.cancelLabel = "Annulla";
    			Alert.noLabel = "No";
    			Alert.show( "Conferma operazione",
                         "Conferma cancellazione", 
                         Alert.YES| Alert.NO,
                         null,
                         handleClose,
                         null,
                         Alert.YES);
		}
		
		public function handleClose(eve:CloseEvent):void{
			if(eve.detail == 1){
			   if(responder != null) {
			   	responder.token = ContextManager.getInstance().service.restCall(module, Persona.MODULE_NAME, id);
			  	ContextManager.getInstance().context.contextLoader.showLoading("Cancellazione documento "+ id);
			   	responder.addEventListener(ResultEvent.RESULT, localResultHandler);
			   	responder.addEventListener(FaultEvent.FAULT, localFaultEvent);
			   } 
			}
		}
		
		private function localResultHandler(e:ResultEvent):void{
			ContextManager.getInstance().context.contextLoader.removeLoading();
		}
		private function localFaultEvent(e:FaultEvent):void{
			ContextManager.getInstance().context.contextLoader.removeLoading();
		}

		private var _cancelFunction:Function;
				private var _id:String;

		public function get cancelFunction():Function
		{
			return _cancelFunction;
		}
		public function set cancelFunction(val:Function):void
		{
			_cancelFunction = val;
		}

		public function get id():String
		{
			return _id;
		}

		

		public function set id(val:String):void
		{
			_id = val;
		}

		public function set responder(val:CallResponder):void
		{
			_responder = val;
		}

		public function get responder():CallResponder
		{
			return _responder;
		}

		public function set module(val:Class):void
		{
			_module = val;
		}

		public function get module():Class
		{
			return _module;
		}
	}
}